home *** CD-ROM | disk | FTP | other *** search
/ Future Workshop / Future Workshop.iso / utility / cenvid / winclip.cmm < prev    next >
Encoding:
Text File  |  1994-05-29  |  7.0 KB  |  221 lines

  1. //***************************************************
  2. //*** WinClip - Command-line access to Windows    ***
  3. //*** ver.1     clipboard text functions from DOS ***
  4. //***************************************************
  5.  
  6. #include <WinClip.lib>
  7.  
  8. main(argc,argv)
  9. {
  10.    Cmd = argv[1];
  11.    Text = argv[2];
  12.    success = False;  // assume failure
  13.    if ( argc == 2 ) {
  14.       if      ( !stricmp(Cmd,"DELETE") )     success = DeleteClipBrd();
  15.       else if ( !stricmp(Cmd,"GET") )        success = GetClipBrd();
  16.       else if ( !stricmp(Cmd,"ISORT") )      success = SortClipBrd(False);
  17.       else if ( !stricmp(Cmd,"LOWER") )      success = LowercaseClipBrd();
  18.       else if ( !stricmp(Cmd,"QUERY") )      success = QueryClipBrd();
  19.       else if ( !stricmp(Cmd,"SORT") )       success = SortClipBrd(True);
  20.       else if ( !stricmp(Cmd,"TEXT") )       success = TextOnlyInClipBrd();
  21.       else if ( !stricmp(Cmd,"UPPER") )      success = UppercaseClipBrd();
  22.       else Instructions();
  23.    } else if ( argc == 3 ) {
  24.       if ( !stricmp(Cmd,"GET") && Text[0] == '@' )
  25.          success = GetClipBrd(Text+1);
  26.       else {
  27.          if ( Text[0] == '@' ) {
  28.             // Get text from FileSpec
  29.             Text = ReadTextFromFile(Text+1);
  30.          }
  31.          if      ( !stricmp(Cmd,"APPEND") )     success = AppendToClipBrd(Text);
  32.          else if ( !stricmp(Cmd,"FIND") )       success = FindInClipboard(Text,True);
  33.          else if ( !stricmp(Cmd,"IFIND") )      success = FindInClipboard(Text,False);
  34.          else if ( !stricmp(Cmd,"PREPEND") )    success = PrependToClipBrd(Text);
  35.          else if ( !stricmp(Cmd,"PUT") )        success = PutInClipBrd(Text);
  36.          else Instructions();
  37.       }
  38.    } else
  39.       Instructions();
  40.    return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
  41. }
  42.  
  43. Instructions()
  44. {
  45.    puts("\a")
  46.    puts("SYNTAX: WinClip [ DELETE | GET | ISORT | LOWER | QUERY | SORT | TEXT | UPPER ]")
  47.    puts("    or  WinClip [ APPEND | FIND | IFIND | PREPEND | PUT ] <text | @<file>>")
  48.    puts("    or  WinClip GET @<file>")
  49.    puts("")
  50.    puts("Where: APPEND - Append new text to end of existing clipboard")
  51.    puts("       DELETE - Delete contents of the clipboard")
  52.    puts("       FIND - Find case-sensitive string in clipboard")
  53.    puts("       GET - Display contents of clipboard; or write to @<file>")
  54.    puts("       IFIND - Find case-insensitive string in clipboard")
  55.    puts("       ISORT - Case-insenstive, alphabetic sort of clipboard contents")
  56.    puts("       PREPEND - Prepend new text to beginning of existing clipboard")
  57.    puts("       PUT - Put text in clipboard")
  58.    puts("       QUERY - Does clipboard contain text?")
  59.    puts("       SORT - Case-sensitive, alphabetic sort of clipboard contents")
  60.    puts("       TEXT - Remove all clipboard types except for text")
  61.    puts("       @<FILE> - file specification to get text from/copy-to")
  62.    puts("")
  63.    printf("Return: Return ERRORLEVEL %d if success, and %d if error. QUERY, FIND, and\n",EXIT_SUCCESS,EXIT_FAILURE)
  64.    printf("        IFIND return %d if found, and %d if not found\n",EXIT_SUCCESS,EXIT_FAILURE)
  65.    puts("");
  66.    puts(`Examples: WinClip DELETE                   Delete clipboard contents`)
  67.    puts(`          WinClip GET @CLIP.TXT            Save to file CLIP.TXT`)
  68.    puts(`          WinClip Append "Little bunny."   Add text to clipboard`)
  69.    puts(`          WinClip GET PRN:                 Send clipboard to printer`)
  70. }
  71.  
  72. ReadTextFromFile(pFileSpec)   // return string; else exit
  73. {
  74.    if ( !(fp = fopen(pFileSpec,"rb")) ) {
  75.       printf("Unable to open file \%s\" for reading.\n",pFileSpec);
  76.       exit(EXIT_FAILURE);
  77.    }
  78.    // read in file CHUNK_SIZE bytes at a time
  79.    #define CHUNK_SIZE 1000
  80.    lText = "", lTextLen = 0;
  81.    do {
  82.       lRead = fread(lChunk,CHUNK_SIZE,fp);
  83.       memcpy(lText+lTextLen,lChunk,lRead);
  84.       lTextLen += lRead;
  85.    } while (CHUNK_SIZE == lRead);
  86.    fclose(fp);
  87.    lText[lTextLen] = '\0';
  88.    return lText;
  89. }
  90.  
  91. AppendToClipBrd(pText)
  92. {
  93.    if ( !(data = GetClipboardText()) )
  94.       data = "";
  95.    strcat(data,pText);
  96.    return PutClipboardText(data);
  97. }
  98.  
  99. DeleteClipBrd()
  100. {
  101.    return ( PutClipboardText(NULL) );
  102. }
  103.  
  104. FindInClipboard(pText,pCaseSensitive)
  105. {
  106.    lTextLen = strlen(pText);
  107.    if ( pCaseSensitive ) {
  108.       lFindChars[0] = pText[0], lFindChars[1] = '\0';
  109.       lFindFunction = "memcmp";
  110.    } else {
  111.       lFindChars[2] = '\0';
  112.       lFindChars[1] = tolower(pText[0]);
  113.       lFindChars[0] = toupper(pText[0]);
  114.       lFindFunction = "memicmp";
  115.    }
  116.    if ( data = GetClipboardText() ) {
  117.       while ( data = strpbrk(data,lFindChars) ) {
  118.          if ( !function(lFindFunction,data,pText,lTextLen) ) {
  119.             printf("Text found.\n");
  120.             return True;
  121.          }
  122.          data++;
  123.       }
  124.    }
  125.    printf("Text not found.\n");
  126.    return False;
  127. }
  128.  
  129. GetClipBrd(OptionalFileSpec)
  130. {
  131.    if ( data = GetClipboardText() ) {
  132.       if ( va_arg() ) {
  133.          if ( !(fp = fopen(OptionalFileSpec,"wb")) ) {
  134.             printf("Error opening file \"%s\" for writing.\n",OptionalFileSpec);
  135.             data = NULL;
  136.          } else {
  137.             fwrite(data,strlen(data),fp);
  138.             fclose(fp);
  139.          }
  140.       } else
  141.          fwrite(data,strlen(data),stdout);
  142.    }
  143.    return(data != NULL);
  144. }
  145.  
  146. LowercaseClipBrd()
  147. {
  148.    if ( data = GetClipboardText() )
  149.       strlwr(data);
  150.    return( data != NULL && PutClipboardText(data));
  151. }
  152.  
  153. PrependToClipBrd(pText)
  154. {
  155.    if ( !(data = GetClipboardText()) )
  156.       data = "";
  157.    strcat(pText,data);
  158.    return PutClipboardText(pText);
  159. }
  160.  
  161. PutInClipBrd(pText)
  162. {
  163.    return PutClipboardText(pText);
  164. }
  165.  
  166. QueryClipBrd()
  167. {
  168.    return ( NULL != GetClipboardText() );
  169. }
  170.  
  171. SortClipBrd(CaseSensitive)
  172. {
  173.    if ( data = GetClipboardText() ) {
  174.  
  175.       // determine if the last character is line-feed, so we'll
  176.       // know whether to add line-feed in the sorted list
  177.       AddFinalLineFeed = ( NULL != strchr("\r\n",data[strlen(data)-1]) );
  178.  
  179.       // build array of each line of text in the clipboard
  180.       LineCount = 0;
  181.       Line = strtok(data,"\r\n");
  182.       while ( Line ) {
  183.          strcpy(List[LineCount++],Line);
  184.          Line = strtok(NULL,"\r\n");
  185.       }
  186.  
  187.       // sort this array of text alphabetically, case-(in)sensitive
  188.       qsort(List,LineCount,CaseSensitive ? "strcmp" : "stricmp" );
  189.  
  190.       // build one long string to put data back into the clipboard
  191.       data[0] = '\0';
  192.       for ( i = 0; i < LineCount; i++ ) {
  193.          strcat(data,List[i]);
  194.          strcat(data,"\r\n");
  195.       }
  196.  
  197.       // if there wasn't supposed to be a line-feed at the end, then remove it
  198.       if ( !AddFinalLineFeed )
  199.          data[strlen(data)-2] = '\0';
  200.  
  201.       // put this new data string in clipboard, replacing old
  202.       if ( !PutClipboardText(data) )
  203.          data = NULL;
  204.    }
  205.    return(data != NULL);
  206. }
  207.  
  208. TextOnlyInClipBrd()
  209. {
  210.    return ( NULL != (data=GetClipboardText())
  211.          && PutClipboardText(data) );
  212. }
  213.  
  214. UppercaseClipBrd()
  215. {
  216.    if ( data = GetClipboardText() )
  217.       strupr(data);
  218.    return( data != NULL && PutClipboardText(data));
  219. }
  220.  
  221.